<!DOCTYPE html>
<html>
<head>
    <title>添加药品--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/style.css"/>
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>
    <script type="text/javascript" src="../../Js/ckeditor/ckeditor.js"></script>


    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
        $(function () {
            $('#backid').click(function () {
                window.location.href = "index.html";
            });

            loadData();
        });

        String.prototype.GetValue = function (para) {
            let reg = new RegExp("(^|&)" + para + "=([^&]*)(&|$)");
            let r = this.substr(this.indexOf("\?") + 1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }

        function loadData() {

            let url = document.location.toString();
            let drId = url.GetValue("drId");
            if (drId != null || drId != undefined && drId != "") {
                $.ajax({
                    url: `/drug/${drId}`,
                    type: "GET",
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 200) {
                            let obj = res.obj;
                            $("input[name='drId']").val(obj.drId);
                            // $("input[name='drUrl']").val(obj.drUrl);
                            $("input[name='drInprice']").val(obj.drInprice);
                            $("input[name='drOutprice']").val(obj.drOutprice);
                            $("input[name='drName']").val(obj.drName);

                            $(`input[name='drType'][value='${obj.drType}']`).prop("checked", true);
                            $(`input[name='drType'][value!='${obj.drType}']`).prop("checked", false);

                            $("input[name='drSimdesc']").val(obj.drSimdesc);
                            $("input[name='drTime']").val(obj.drTime);
                            document.getElementById('drDetadesc').value = obj.drDetadesc;
                            document.getElementById('drFatory').value = obj.drFatory;
                            $("input[name='drDirection']").val(obj.drDirection);
                            document.getElementById('drRemark').value = obj.drRemark;
                        }
                    }
                })
            }
        }

        function save() {
            // 不使用表单方式提交请求
            event.preventDefault();
            // 获取请求参数
            let drId = $("input[name='drId']").val();
            let drUrl = $("input[name='drUrl']").val();
            let drInprice = $("input[name='drInprice']").val();
            let drOutprice = $("input[name='drOutprice']").val();
            let drName = $("input[name='drName']").val();
            let drType = $("input[name='drType']:checked").val();
            let drSimdesc = $("input[name='drSimdesc']").val();
            let drTime = $("input[name='drTime']").val();
            let drDetadesc = document.getElementById('drDetadesc').value;
            let drFatory = document.getElementById('drFatory').value;
            let drDirection = $("input[name='drDirection']").val();
            let drRemark = document.getElementById('drRemark').value;

            $.ajax({
                type: "POST",
                url: "/drug/save",
                dataType: "json",
                data: {
                    drId, drUrl, drInprice, drOutprice, drName, drType,
                    drSimdesc, drTime, drDetadesc, drFatory, drDirection, drRemark
                },
                success: function (res) {
                    alert(res.msg);
                    if (res.code === 200) {
                        // 跳转至查询页
                        $(location).attr("href", "index.html");
                    }
                }
            });
        }
    </script>
</head>
<body>
<form action="index.html" method="post" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <!--<tr>
            <td width="10%" class="tableleft">药品名称</td>
            <td><input type="text" name="drName" value=""/></td>
        </tr>-->
        <input type="hidden" name="drId" value="">
        <tr>
            <td width="10%" class="tableleft">图片</td>
            <td><input type="file" name="drUrl" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">进价</td>
            <td><input type="text" name="drInprice" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">售价</td>
            <td><input type="text" name="drOutprice" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">药品名称</td>
            <td><input type="text" name="drName" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">药品类型</td>
            <td><input type="radio" name="drType" value="1" checked/>处方药&nbsp;&nbsp;&nbsp;<input type="radio"
                                                                                                 name="drType"
                                                                                                 value="2"/>中药&nbsp;&nbsp;&nbsp;<input
                    type="radio" name="drType" value="3"/>西药
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">简单描述</td>
            <td><input type="text" name="drSimdesc" value=""/></td>
        </tr>

        <tr>
            <td width="10%" class="tableleft">保质期</td>
            <td><input type="text" name="drTime" value="36"/>月</td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">详细描述</td>
            <td><textarea id="drDetadesc"></textarea></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">生产厂商</td>
            <td><textarea id="drFatory"></textarea></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">服用说明</td>
            <td><input type="text" name="drDirection" value=""/></td>
        </tr>

        <tr>
            <td width="10%" class="tableleft">备注</td>
            <td><textarea id="drRemark"></textarea></td>
        </tr>
        <tr>
            <td colspan="2">
                <center>
                    <button onclick="save()" class="btn btn-primary" type="button">保存</button> &nbsp;&nbsp;<button
                        type="button" class="btn btn-success" name="backid" id="backid">返回列表
                </button>
                </center>
            </td>
        </tr>
    </table>
</form>
</body>
</html>